package com.asset.dispoal.business.bsEvidenceRecognition.service.commom;

import com.asset.dispoal.business.bsEvidenceRecognition.domain.db.mapper.BusinessDataManageMapper;
import com.asset.dispoal.business.bsEvidenceRecognition.dto.ContractEvidenceRequest;
import com.asset.dispoal.business.bsEvidenceRecognition.dto.ContractEvidenceResponse;
import com.asset.dispoal.business.bsEvidenceRecognition.dto.EvidenceBySingleContractDo;
import com.asset.dispoal.business.bsEvidenceRecognition.service.converter.EvidenceBySingleConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Component
@Slf4j
public class QuerySingleContractEvidenceCommand implements BaseCommand<ContractEvidenceRequest, Result<List<ContractEvidenceResponse>>> {


    @Autowired
    BusinessDataManageMapper businessDataManageMapper;

    @Autowired
    private EvidenceBySingleConverter evidenceBySingleConverter;

    @Override
    public Result<List<ContractEvidenceResponse>> execute(ContractEvidenceRequest request) {
        List<ContractEvidenceResponse> response = businessDataManageMapper.queryEvidenceTypeByAssetInstBatchId(request.getAssetInstBatchId(), null, null);
        List<Map<String, String>> dataDetailList = businessDataManageMapper.queryDataDetail(request.getAssetInstBatchId());
        Map<String, Object> contractMap = businessDataManageMapper.queryContractResult(request.getContractId(), dataDetailList);
        contractMap.put("contractId", request.getContractId());
        List<EvidenceBySingleContractDo> evidenceBySingleContractDos = businessDataManageMapper.querySingleContractEvidenceDo(contractMap, request.getAssetInstBatchId());
        Set<String> removeRecoginitionIdSet = businessDataManageMapper.queryRemoveIdByContractId(request.getContractId());
        evidenceBySingleContractDos = evidenceBySingleContractDos.stream().filter(evidence -> !removeRecoginitionIdSet.contains(evidence.getId())).collect(Collectors.toList());
        for (ContractEvidenceResponse evidenceType : response) {
            List<EvidenceBySingleContractDo> matchDoList = evidenceBySingleContractDos.stream().filter(evidence -> evidence.getEvidenceTypeId().equals(evidenceType.getId())).collect(Collectors.toList());
            evidenceType.setValue(evidenceBySingleConverter.doListToResList(matchDoList));
        }
        return Result.ok(response);
    }
}
